package de.tabuma.ssoweb.view;

import de.tabuma.lib.tabumaview;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;
import java.util.*;
import java.util.logging.*;

/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.1 $
 * @since $Date: 2009/12/30 01:25:37 $
 */
public class bottom extends tabumaview {

    private static Logger log = Logger.getLogger(bottom.class.getName());

    public bottom(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response,PrintWriter out)  {
        try {
            RequestDispatcher disp1 = context.getRequestDispatcher("/intern/bottom1.html");
            disp1.include(request, response);

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();
            String logoutUrl = "";
            if(request.getLocale().equals(Locale.GERMAN)  || request.getLocale().equals(Locale.GERMANY)) {
                logoutUrl = userService.createLogoutURL("/de/index.html");
            }
            else {
                logoutUrl = userService.createLogoutURL("/en/index.html");
            }
            logoutUrl = URLDecoder.decode(logoutUrl, "UTF8");

            //Converting the logout Url into formular fields
            int indexLogoutUrl = logoutUrl.indexOf('?');
            String url = logoutUrl.substring(0,indexLogoutUrl);

            out.println("<form action=\""+url+"\">");

            String split[] = logoutUrl.substring(indexLogoutUrl+1).split("&");
            if(split!=null) {
                for(int i=0;i<split.length;i++) {
                    String split1[] = split[i].split("=");
                    out.println("\t<input type=\"hidden\" name=\""+split1[0]+"\" value=\""+split1[1]+"\"/>");
                }
            }

            out.println("\t<input type=\"submit\" class=\"loginbutton\" value=\"logout\"/>");
            out.println("</form>");

            disp1 = context.getRequestDispatcher("/intern/bottom2.html");
            disp1.include(request, response);
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
    }

}
